package server.alarmThreads;

import java.util.concurrent.ConcurrentLinkedQueue;

import server.container.EinsatzAlarm;
import server.db.SQL;

public class AlarmDbUpdateThread implements AlarmThread {

	private static ConcurrentLinkedQueue<EinsatzAlarm> list = new ConcurrentLinkedQueue<EinsatzAlarm>();

	public AlarmDbUpdateThread() {

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {

				boolean isRunning = true;

				while (isRunning) {

					synchronized (list) {
						try {
							if (list.size() == 0) {
								list.wait(1000);
							}
						} catch (InterruptedException e) {
						}

						EinsatzAlarm alarm = list.poll();
						if (alarm != null) {
							SQL.getInstance().alarmierungLoggen(alarm);
						}

					}
					Thread.yield();

				}

			}
		});
		t.setPriority(Thread.MIN_PRIORITY);
		t.setDaemon(true);
		t.start();

	}

	@Override
	public void alarmiere(EinsatzAlarm alarm) {

		list.add(alarm);
		synchronized (list) {
			list.notifyAll();
		}

	}

}
